package com.wugh.fast.security.social.config;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.social.connect.ApiAdapter;
import org.springframework.social.connect.support.OAuth2ConnectionFactory;
import org.springframework.social.oauth2.OAuth2ServiceProvider;

/**
 * 说明:
 *
 * @author wugh
 * @date 2018-10-30 16:17
 */
public abstract class AccessTokenConnectFactory<S> extends OAuth2ConnectionFactory<S> {
    public AccessTokenConnectFactory(String providerId, OAuth2ServiceProvider<S> serviceProvider, ApiAdapter<S> apiAdapter) {
        super(providerId, serviceProvider, apiAdapter);
    }

    @Override
    public String generateState() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

//        if (authentication != null && authentication instanceof OAuth2Authentication) {
//            OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authentication.getDetails();
//            String accessToken = details.getTokenValue();
//            JSONObject json = new JSONObject();
//            json.put("access_token", accessToken);
//            return CodecUtils.encodeBase64(json.toJSONString());
//        }
        return super.generateState();
    }
}
